package com.xonro.common.histrans.sfeg.biz.cm;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.sfeg.biz.CommTransBiz;
import com.xonro.common.histrans.sfeg.biz.common.SfegRecordTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:JF
 * @Description:合同档案表数据迁移
 * @Date:2022/6/2 11:02
 * @parm: * @param null
 */
public class ContractTransBiz extends SfegRecordTransBiz {

    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_MDM_CONTRACT");//合同档案表数据
        logModel.setOuterTable("tb_ContractInfo");//合同信息
        logModel.setOuterPK(mainData.getString("ctCode"));
        logModel.setOuterExt1(mainData.getString("ctCode"));
        logModel.setOuterExt2(mainData.getString("HTBH"));
        logModel.setOuterExt3(mainData.getString("ctType"));//合同类型
        BOCreateData mainBOCreateData = new BOCreateData();

        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("CONTRACT_TYPE", mainData.getString("ctType"));//合同类型
        mainBO.set("CONTRACT_NO", mainData.getString("ctCode"));//合同编号
        mainBO.set("CONTRACT_NAME", mainData.getString("ctName"));//合同名称
        mainBO.set("LINK_MAIN_TYPE_NO", mainData.getString("mainContractCode"));//关联主合同编号
        mainBO.set("DEPT_NAME", mainData.getString("dealDept"));//经办部门
        mainBO.set("OPERATOR", CommTransBiz.getInstance().getUserIdByName( mainData.getString("dealUser")));//经办人
        mainBO.set("COMPANY_NO", mainData.getDate("belongCorp"));//所属单位编号
        mainBO.set("NO_TAX_CONTRACT_AMOUNT", mainData.getString("signTotal"));//签约金额(不含税)
        if (StringUtils.isNotBlank(mainData.getString("inureDate")) && mainData.getString("inureDate").length() == 10) {
            try {
                mainBO.set("EFFECTIVE_DATE", DateUtil.parse(mainData.getString("inureDate"), "yyyy-MM-dd"));//生效日期
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        mainBO.set("FORMULATE_PERSON", CommTransBiz.getInstance().getUserId( mainData.getString("drawUser")));//拟定人
        if (StringUtils.isNotBlank(mainData.getString("begDate")) && mainData.getString("begDate").length() == 10) {
            try {
                mainBO.set("CONTRACT_START_DATE", DateUtil.parse(mainData.getString("begDate"), "yyyy-MM-dd"));//合同开始日期
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (StringUtils.isNotBlank(mainData.getString("endDate")) && mainData.getString("endDate").length() == 10) {
            try{
            mainBO.set("CONTRACT_END_DATE", DateUtil.parse(mainData.getString("endDate"), "yyyy-MM-dd"));//合同结束日期
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        mainBO.set("SIGN_ADDRESS", mainData.getString("signAddress"));//签订地点
        mainBO.set("REMARK", mainData.getString("bz"));//备注
        mainBO.set("DEPT_NO", mainData.getString("fillDept"));//备注
        mainBO.set("LINK_MAIN_TYPE_NO", mainData.getString("SSHT"));//关联主合同编号
        mainBO.set("MAIN_TYPE", mainData.getString("HTXS"));//合同主类型
        mainBO.set("IS_SUPPLEMENT", mainData.getString("bcsctcode"));//是否补充合同
        mainBO.set("PROJECT_NO", mainData.getString("pjCode"));//項目编号
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);


        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //OA表订单编号
        fileTransData.setFkCode(mainData.getString("ctCode"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_MDM_CONTRACT");
        fileTransData.setBoName("BO_XR_MDM_CONTRACT");
        fileTransData.setFieldName("ATTACHMENT");


    }


    @Override
    protected String getProcessDefId() {
        return "obj_9ce3bca35bef47e5816f687231a92f6f";
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }

    //档案表没有流程
    @Override
    protected String getActivityDefId() {
        return null;
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        String htCode = mainData.getString("ctCode");//合同代码
        String htbh = mainData.getString("HTBH");//合同编号
        return htCode + htbh;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("ctCode");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_ContractInfo";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("ctCode");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM tb_ContractInfo WHERE belongCorp = '1040' ");
    }
}
